package app.exp;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import org.rygh.core.html.table.TableData;
import org.rygh.core.html.table.TableTuple;
import org.rygh.core.model.common.MapList;
import org.rygh.core.util.UIo;
import org.rygh.core.util.tex.TexTabular;

import wsi.script.AppConfig;
import wsi.script.AppXml;
import app.App;

public class exp4MergePreTime extends App {
	static class Tuple {
		String name;
		double time;

		@Override
		public String toString() {
			return name;
		}
	}

	static MapList<String, String> nameMap = new MapList<>();
	static String[] order = { "album", "book", "city", "country", "movie",
			"person", "publisher", "song" };

	static {
		nameMap.put("city", "wordnet_administrative_district_108491826_label");
		nameMap.put("movie", "wordnet_movie_106613686_hasPreferredName",
				"wordnet_movie_106613686_hasLabel");
		nameMap.put("person", "wordnet_person_100007846_hasPreferredName",
				"wordnet_person_100007846_hasLabel", "MusicArtist_name");
		nameMap.put("book", "wordnet_book_106410904_hasPreferredName");
		nameMap.put("album", "wordnet_album_106591815_hasPreferredName",
				"Release_label");
		nameMap.put("song", "wordnet_song_107048000_hasPreferredName",
				"Track_label");
		// nameMap.put("wordnet_country_108544813_hasTLD", null);
		nameMap.put("country", "wordnet_country_108544813_label");
		nameMap.put("publisher", "wordnet_publisher_108062623_hasPreferredName");
	}

	private static Tuple find(List<String> key, List<Tuple> tuples) {
		for (Tuple t : tuples)
			for (String k : key)
				if (t.name.startsWith(k))
					return t;

		// System.out.println("[info] find " + key + tuples);

		return null;
	}

	public static void main(String[] args) {
		init();

		AppConfig.init();

		parse(AppXml.YAGO);
		parse(AppXml.MBZ);
		parse(AppXml.IMDB);

		bye();
	}

	private static List<Tuple> parse(File dir, boolean degree) {
		File file = new File(dir, String.format("timers%s.txt",
				degree ? "_degree" : ""));

		List<Tuple> tuples = new LinkedList<>();

		String[] lines = UIo.readFile(file).split("\r\n");
		int pos;

		assert lines.length > 0 : "[info] lines " + file.getAbsolutePath()
				+ UIo.readFile(file);

		for (String line : lines) {
			pos = line.lastIndexOf(':');

			if (pos == -1)
				continue;

			Tuple t = new Tuple();
			t.name = line.substring(0, pos);
			t.time = Double.parseDouble(line.substring(pos + 1,
					line.length() - 1));

			tuples.add(t);
		}

		// System.out.println("[info] tuples " + tuples);
		return tuples;
	}

	private static void parse(String kdb) {
		AppConfig ac = AppConfig.shared();

		File dir = new File(ac.getResourcesFolder().getParent(), kdb + "/input");
		assert dir.isDirectory();

		TableData dataset = new TableData();
		List<Tuple> timers = parse(dir, false);
		List<Tuple> dtimers = parse(dir, true);
		Tuple t, dt;
		TableTuple tt;

		tt = new TableTuple();
		tt.add("Type", "Rnd", "Deg", "FB");
		dataset.add(tt);

		for (String o : order) {
			List<String> k = nameMap.get(o);

			t = find(k, timers);
			dt = find(k, dtimers);

			if (t == null || dt == null)
				continue;

			tt = new TableTuple();
			dataset.add(tt);

			tt.add(o);
			tt.add(String.format("%.2f", t.time));
			tt.add(String.format("%.2f", dt.time));
			tt.add(0);
		}

		//

		File tex_dir = new File(
				"/home/nanmus/Data/Dropbox/Shared/ClementThesis/Documents/Paper/exp/");

		UIo.saveFile(new File(tex_dir, String.format("exp1_pre_%s.tex", kdb)),
				new TexTabular(dataset).toTexBodyString());
	}
}
